#ifndef SAVE_FILE_H
#define SAVE_FILE_H
#include <sys/stat.h>
#include <fcntl.h>
#include <vector>
#include <chrono>
#include <string>
#include <wx/datetime.h>
#include <wx/sizer.h>
#ifdef _MSC_VER
#include <format>
#define fmt std
#else
#include <fmt/format.h>
#endif
#include <exception>
#include <filesystem>
#include <lz4.h>
#include <wx/progdlg.h>
#include <vector>
#include "CompressThread.h"
#include "PixelFormat.h"

class SaveFile
{
public:
	SaveFile();
	virtual ~SaveFile();
	void SetSavePath(std::string _path);
	std::string GetCurrentFileName();
	void SetImageSize(int32_t width, int32_t height);
	int32_t GetImageCount();
	wxSize GetImageSize();
	bool GetFileStatus();
	bool GetFileOpenStatus();
  
	//图像序列写入
	std::string StartInputImage(int32_t format,int32_t width,int32_t height);
	void InputImage(const unsigned char* imageData,size_t imageSize,uint64_t timestamp=0);
	void StopInputImage();
	//图像序列读取
	void StartReadImage(const std::string& fileName,int32_t& width,int32_t& height,int32_t& frameCount,pixelFormat& format);
	unsigned char* ReadImage();
	void ReadImageWithTimestamp(unsigned char*  &_image,uint64_t& _timestamp);
	void SetFramePos(long Pos);
	void StopReadImage();
	//解压图片
	bool DecompressionFile(std::string fileName,wxWindow* display);
private:
	void CreateReadIndex(); //创建读取索引
	bool CreateFile();
	std::string path;
	std::string fileName;
	int32_t width;
	int32_t height;
	int32_t ImageCount;
	int fileFD=0; //文件描述符
  pixelFormat ImageFormat; //文件格式
	//解压压缩
	char* doubleBuff[2];
	char* cmpBuff;
	long buffPoint=0;
	LZ4_stream_t* lz4Stream_body;
	LZ4_streamDecode_t* lz4StreamDecode_body;
	long isize=0;
	//访问索引
	std::vector<long> readPosIndex;
};

#endif // !SAVE_FILE_H
